package com.jianyun.jyzs.activity;

import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.qrcode.qrcodemanager.QRCodeManager;
import com.jianyun.jyzs.receive.NetworkConnectChangedReceiver;
import com.jianyun.jyzs.utils.downtime.DownTimer;
import com.jianyun.jyzs.utils.downtime.DownTimerListener;

import cn.rongcloud.rce.lib.BooleanResultCallback;
import cn.rongcloud.rce.lib.QRCodeTask;
import cn.rongcloud.rce.lib.RceErrorCode;

/**
 * 二维码扫描登录
 * Created by yangshijun on 2018/6/14.
 */

public class LoginToComputerActivity extends AppCompatActivity  implements DownTimerListener, NetworkConnectChangedReceiver.NetworkChangeStatusListener {


    private String token;
    private TextView loginTextStatus;
    private boolean isInvalid;
    private boolean isHasNetwork;
    private NetworkConnectChangedReceiver mNetworkConnectChangedReceiver;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rce_activity_login_to_computer);
        loginTextStatus = (TextView) findViewById(R.id.qr_login_txt);
        findViewById(R.id.cancel_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.qr_pager_finish).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.qr_login_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isInvalid) {
                    Toast.makeText(LoginToComputerActivity.this, R.string.rce_qr_token_invalid, Toast.LENGTH_SHORT).show();
                    return;
                }
                if (isHasNetwork) {
                    QRCodeTask.getInstance().confirmLogin(token, new BooleanResultCallback() {
                        @Override
                        public void onTrueOnUiThread() {
                            finish();
                        }

                        @Override
                        public void onFalseOnUiThread(RceErrorCode errorCode) {
                            super.onFalseOnUiThread(errorCode);
                        }
                    });
                } else {
                    Toast.makeText(LoginToComputerActivity.this, getString(R.string.rce_qr_network_fail1) + "," + getString(R.string.rce_qr_network_fail2), Toast.LENGTH_SHORT).show();
                }
            }
        });
        token = getIntent().getStringExtra(QRCodeManager.LOGIN_TOKEN);
        final String devices = getIntent().getStringExtra(QRCodeManager.LOGIN_PLATFORM);
        switchText(getPlatformType(devices));
        DownTimer downTimer = new DownTimer();
        downTimer.setListener(this);
        downTimer.startDown(300 * 1000);

        IntentFilter filter = new IntentFilter();
        filter.addAction(NetworkConnectChangedReceiver.CONNECTIVITY_CHANGE);
        mNetworkConnectChangedReceiver = new NetworkConnectChangedReceiver();
        mNetworkConnectChangedReceiver.setNetworkChangeStatusListener(this);
        registerReceiver(mNetworkConnectChangedReceiver, filter);
    }

    @Override
    public void onTick(long millisUntilFinished) {

    }

    @Override
    public void onFinish() {
        isInvalid = true;
        QRCodeTask.getInstance().resetToken(token, null);
    }

    private QRCodeManager.PlatformType getPlatformType(String type) {
        QRCodeManager.PlatformType[] types = QRCodeManager.PlatformType.values();
        for (int i = 0; i < types.length; i++) {
            if (types[i].getPlatformType().equals(type)) {
                return types[i];
            }
        }
        throw new IllegalArgumentException("can not find platformType of : " + type);
    }

    @Override
    public void onChange(boolean isHasNetwork) {
        this.isHasNetwork = isHasNetwork;
    }

    private void switchText(QRCodeManager.PlatformType platformType) {
        switch (platformType) {
            case Mac:
                loginTextStatus.setText(R.string.rce_qr_login_status_mac);
                break;
            case Windows:
                loginTextStatus.setText(R.string.rce_qr_login_status_windows);
                break;
            case Web:
                loginTextStatus.setText(R.string.rce_qr_login_status_web);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mNetworkConnectChangedReceiver);
    }
}
